package com.badminton.commendSys.service.impl;

import java.util.List;
import com.badminton.HubSys.domain.EquipmentHub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.badminton.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.badminton.commendSys.mapper.EquipmentrecommendationMapper;
import com.badminton.commendSys.domain.Equipmentrecommendation;
import com.badminton.commendSys.service.IEquipmentrecommendationService;

/**
 * 装备推荐Service业务层处理
 *
 * @date 2024-07-04
 */
@Service
public class EquipmentrecommendationServiceImpl implements IEquipmentrecommendationService {
    @Autowired
    private EquipmentrecommendationMapper equipmentrecommendationMapper;

    /**
     * 查询装备推荐
     *
     * @param recommendationid 装备推荐主键
     * @return 装备推荐
     */
    @Override
    public Equipmentrecommendation selectEquipmentrecommendationByRecommendationid(Long recommendationid) {
        return equipmentrecommendationMapper.selectEquipmentrecommendationByRecommendationid(recommendationid);
    }

    /**
     * 查询装备推荐列表
     *
     * @param equipmentrecommendation 装备推荐
     * @return 装备推荐
     */
    @Override
    public List<Equipmentrecommendation> selectEquipmentrecommendationList(Equipmentrecommendation equipmentrecommendation) {
        return equipmentrecommendationMapper.selectEquipmentrecommendationList(equipmentrecommendation);
    }

    /**
     * 新增装备推荐
     *
     * @param equipmentrecommendation 装备推荐
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEquipmentrecommendation(Equipmentrecommendation equipmentrecommendation) {
        int rows = equipmentrecommendationMapper.insertEquipmentrecommendation(equipmentrecommendation);
        insertEquipmentHub(equipmentrecommendation);
        return rows;
    }

    /**
     * 修改装备推荐
     *
     * @param equipmentrecommendation 装备推荐
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEquipmentrecommendation(Equipmentrecommendation equipmentrecommendation) {
        equipmentrecommendationMapper.deleteEquipmentHubByEquipmentID(equipmentrecommendation.getRecommendationid());
        insertEquipmentHub(equipmentrecommendation);
        return equipmentrecommendationMapper.updateEquipmentrecommendation(equipmentrecommendation);
    }

    /**
     * 批量删除装备推荐
     *
     * @param recommendationids 需要删除的装备推荐主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEquipmentrecommendationByRecommendationids(Long[] recommendationids) {
        equipmentrecommendationMapper.deleteEquipmentHubByEquipmentIDs(recommendationids);
        return equipmentrecommendationMapper.deleteEquipmentrecommendationByRecommendationids(recommendationids);
    }

    /**
     * 删除装备推荐信息
     *
     * @param recommendationid 装备推荐主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEquipmentrecommendationByRecommendationid(Long recommendationid) {
        equipmentrecommendationMapper.deleteEquipmentHubByEquipmentID(recommendationid);
        return equipmentrecommendationMapper.deleteEquipmentrecommendationByRecommendationid(recommendationid);
    }

    /**
     * 新增装备列表信息
     *
     * @param equipmentrecommendation 装备推荐对象
     */
    public void insertEquipmentHub(Equipmentrecommendation equipmentrecommendation) {
        List<EquipmentHub> equipmentHubList = equipmentrecommendation.getEquipmentHubList();
        Long recommendationid = equipmentrecommendation.getRecommendationid();
        if (StringUtils.isNotNull(equipmentHubList)) {
            List<EquipmentHub> list = new ArrayList<>();
            for (EquipmentHub equipmentHub : equipmentHubList) {
                equipmentHub.setEquipmentid(recommendationid);
                list.add(equipmentHub);
            }
            if (list.size() > 0) {
                equipmentrecommendationMapper.batchEquipmentHub(list);
            }
        }
    }
}
